package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.bi.BHistoryActivity;
import com.stn.zjx.givingtreasure.activity.bi.EthTransferDetailActivity;
import com.stn.zjx.givingtreasure.bean.EthTransfer;
import com.stn.zjx.givingtreasure.event.RefreshWalletEvent;
import com.stn.zjx.givingtreasure.table.WalletUser;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EthTransferListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<EthTransfer> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public BHistoryActivity context;

   public EthTransferListAdapter(BHistoryActivity context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_eth_transfer, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
                  EthTransfer   ethTransfer= dataBeanList.get(i);
          if(context.address.equals(ethTransfer.getFromAddress())){
              ih.iv.setImageResource(R.mipmap.transfer_out);
              ih.address.setText("["+context.getString(R.string.out_address)+"]"+ethTransfer.getToAddress().substring(0,5)
              +"****"+ethTransfer.getToAddress().substring(ethTransfer.getToAddress().length()-5,ethTransfer.getToAddress().length())
              );
              ih.price.setTextColor((ActivityCompat.getColor(context,R.color.default_red)));
              ih.price.setText("-"+ethTransfer.getAmount()+" "+ethTransfer.getSymbol().toLowerCase());
          }else{
              ih.iv.setImageResource(R.mipmap.transfer_in);
              ih.address.setText("["+context.getString(R.string.in_address)+"]"+ethTransfer.getFromAddress().substring(0,5)
                      +"****"+ethTransfer.getFromAddress().substring(ethTransfer.getFromAddress().length()-5,ethTransfer.getToAddress().length())
              );
              ih.price.setTextColor((ActivityCompat.getColor(context,R.color.default_green)));
              ih.price.setText("+"+ethTransfer.getAmount()+" "+ethTransfer.getSymbol().toLowerCase());
          }
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd/HH:mm:ss");
           ih.dateTime.setText(sdf.format(new Date(Long.parseLong(ethTransfer.getSendTime()))));
          switch (ethTransfer.getStatus()){
              case 0:
                  ih.status.setText(context.getString(R.string.transferLoading));
                  ih.status.setTextColor((ActivityCompat.getColor(context,R.color.default_orange)));
                  break;
              case 1:
                  ih.status.setText(context.getString(R.string.success));
                  ih.status.setTextColor((ActivityCompat.getColor(context,R.color.default_green)));
                  break;
              case  2:
                  ih.status.setText(context.getString(R.string.fail));
                  ih.status.setTextColor((ActivityCompat.getColor(context,R.color.default_red)));
                  break;
          }
          ih.rootView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Bundle bundle=new Bundle();
                  bundle.putString("txHash",ethTransfer.getTxHash());
               RxActivityTool.skipActivity(context, EthTransferDetailActivity.class,bundle);
              }
          });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<EthTransfer> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

    public void appendList(List<EthTransfer> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
          public ImageView iv;
          public TextView address;
          public  TextView dateTime;
        public  TextView status;
        public TextView price;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            iv=itemView.findViewById(R.id.iv);
            address=itemView.findViewById(R.id.address);
            dateTime=itemView.findViewById(R.id.dateTime);
            status=itemView.findViewById(R.id.status);
            price=itemView.findViewById(R.id.price);
        }
   }

}